﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "GameMode/CombatGameMode.h"

#include "Blueprint/UserWidget.h"
#include "Character/PlayerCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "UMG/MainUIWidget.h"
#include "UMG/PauseWidget.h"

ACombatGameMode::ACombatGameMode()
{
	DefaultPawnClass = APlayerCharacter::StaticClass();
}

void ACombatGameMode::BeginPlay()
{
	Super::BeginPlay();
	UUserWidget* UserWidget = CreateWidget(GetWorld(), MainUIClass);
	if (UserWidget)
	{
		UserWidget->AddToViewport();
		MainUI = Cast<UMainUIWidget>(UserWidget);
	}

	if (AudioActorClass)
	{
		AudioActor = Cast<AAudioActor>(GetWorld()->SpawnActor(AudioActorClass));
	}
}

void ACombatGameMode::AddCreatePoint(ACreateEnemyPoint* CreateEnemyPoint)
{
	CreateEnemyPoints.Add(CreateEnemyPoint);
}

void ACombatGameMode::PlayBloodScreenAnim()
{
	if (MainUI)
	{
		MainUI->PlayBloodScreenAnim();
	}
}

void ACombatGameMode::ChangeToGameOverUI(const FText& GameOverDesText)
{
	if (MainUI)
	{
		MainUI->RemoveFromParent();
	}
	UUserWidget* UserWidget = CreateWidget(GetWorld(), GameOverUIClass);
	if (UserWidget)
	{
		UserWidget->AddToViewport();
		GameOverUI = Cast<UGameOverWidget>(UserWidget);
		GameOverUI->GameOverDesText->SetText(GameOverDesText);
		// player停止操作
		APlayerCharacter* PlayerPawn = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
		if (PlayerPawn)
		{
			PlayerPawn->StopOperation();
		}
	}
}

void ACombatGameMode::ChangeToGamePauseUI()
{
	if (MainUI)
	{
		MainUI->RemoveFromParent();
	}
	UUserWidget* UserWidget = CreateWidget(GetWorld(), PauseWidget);
	if (UserWidget)
	{
		UserWidget->AddToViewport();
		GamePauseUI = Cast<UPauseWidget>(UserWidget);
		// player停止操作
		APlayerCharacter* PlayerPawn = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
		if (PlayerPawn)
		{
			PlayerPawn->StopOperation();
		}
	}
}

void ACombatGameMode::BackToMainUI()
{
	if (GameOverUI)
	{
		GameOverUI->RemoveFromParent();
	}
	if (GamePauseUI)
	{
		GamePauseUI->RemoveFromParent();
	}
	if (MainUI)
	{
		MainUI->AddToViewport();
		// player恢复操作
		APlayerCharacter* PlayerPawn = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
		if (PlayerPawn)
		{
			PlayerPawn->StartOperation();
		}
	}
}
